package com.uenpay.im.wxpay;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

import com.uenpay.im.IMApplication;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class PayActivity extends Activity {

	private String time = "", noncestr = "", sign = "",partner_id="";
	private String prepay_id = "";

	PayReq req;
	final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		noncestr = getIntent().getStringExtra("noncestr");
		prepay_id = getIntent().getStringExtra("prepay_id");
		partner_id = getIntent().getStringExtra("partner_id");
		sign = getIntent().getStringExtra("sign");
		time = getIntent().getStringExtra("time");
		req = new PayReq();
		msgApi.registerApp(IMApplication.getInstance().WX_APPID);
		genPayReq();
	}

	private void genPayReq() {
		req.appId = IMApplication.getInstance().WX_APPID;
		req.partnerId = partner_id;
		req.prepayId = prepay_id;
		req.packageValue = "Sign=WXPay";
		req.nonceStr = noncestr;
		req.timeStamp = time;
//		req.timeStamp = String.valueOf(genTimeStamp());
//		req.sign = sign;
//		List<NameValuePair> signParams = new LinkedList<NameValuePair>();
//		signParams.add(new BasicNameValuePair("appid", req.appId));
//		signParams.add(new BasicNameValuePair("noncestr", req.nonceStr));
//		signParams.add(new BasicNameValuePair("package", req.packageValue));
//		signParams.add(new BasicNameValuePair("partnerid", req.partnerId));
//		signParams.add(new BasicNameValuePair("prepayid", req.prepayId));
//		signParams.add(new BasicNameValuePair("timestamp", req.timeStamp));
//		req.sign = genAppSign(signParams);
		req.sign = sign;
		msgApi.registerApp(IMApplication.getInstance().WX_APPID);
		msgApi.sendReq(req);

		finish();
	}

//	private String genAppSign(List<NameValuePair> params) {
//		StringBuilder sb = new StringBuilder();
//
//		for (int i = 0; i < params.size(); i++) {
//			sb.append(params.get(i).getName());
//			sb.append('=');
//			sb.append(params.get(i).getValue());
//			sb.append('&');
//		}
//		sb.append("key=");
//		sb.append(HWLApplication.getInstance().API_KEY);
//
//		String appSign = getMessageDigest(sb.toString().getBytes())
//				.toUpperCase();
//		Log.e("orion", appSign);
//		return appSign;
//	}

//	private String getMessageDigest(byte[] buffer) {
//		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
//				'a', 'b', 'c', 'd', 'e', 'f' };
//		try {
//			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
//			mdTemp.update(buffer);
//			byte[] md = mdTemp.digest();
//			int j = md.length;
//			char str[] = new char[j * 2];
//			int k = 0;
//			for (int i = 0; i < j; i++) {
//				byte byte0 = md[i];
//				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
//				str[k++] = hexDigits[byte0 & 0xf];
//			}
//			return new String(str);
//		} catch (Exception e) {
//			return null;
//		}
//	}

	private long genTimeStamp() {
		return System.currentTimeMillis() / 1000;
	}
}
